package main

import (
	"fmt"
	"runtime"
	"sync"
	"time"
)

//Mutex不与goroutine绑定，不可重入
func main() {
	var mutex sync.Mutex
	//wait := sync.WaitGroup{}
	fmt.Println("Locked")
	//mutex.Lock()
	for i := 1; i <= 5; i++ {
		//wait.Add(1)
		go func(i int) {
			fmt.Println("start:", i)
			mutex.Lock()
			fmt.Println("Lock:", i)
			time.Sleep(time.Second)
			fmt.Println("Unlock:", i)
			mutex.Unlock()
			//defer wait.Done()
		}(i)
	}
	//time.Sleep(time.Second)
	for runtime.NumGoroutine() > 1 {
		time.Sleep(100 * time.Millisecond)
	}
	//fmt.Println("Unlocked")
	//mutex.Unlock()
	//wait.Wait()
}
